package com.hospital.hms.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.hospital.hms.common.CommonResult;
import com.hospital.hms.controller.form.*;
import com.hospital.hms.controller.form.SelectDoctorInfoByIdForm;
import com.hospital.hms.service.DoctorService;
import com.hospital.hms.utils.PageUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/doctor")
@CrossOrigin(origins = "http://localhost:4000", allowCredentials = "true")
@Tag(name = "DoctorController", description = "医生管理接口")
public class DoctorController {

    @Autowired
    DoctorService doctorService;

    @Operation(summary = "分页显示医生信息接口")
    @PostMapping("/selectConditionByPage")
    @SaCheckLogin
    public CommonResult selectConditionByPage(@RequestBody @Valid SelectConditionByPageForm form) {
        Map param = BeanUtil.beanToMap(form);
        int page = form.getPage();
        int length = form.getLength();
        int start = (page - 1) * length;
        param.put("start", start);
        PageUtils pageUtils = doctorService.selectConditionByPage(param);
        return CommonResult.ok().put("result", pageUtils);
    }

    @Operation(summary = "获取医生详细信息接口")
    @PostMapping("/selectDoctorDetailById")
    @SaCheckLogin
    public CommonResult selectDoctorDetailById(@RequestBody @Valid SelectDoctorDetailByIdForm form) {
        return CommonResult.ok().put("doctor", doctorService.selectDoctorDetailById(form.getId()));
    }

    @Operation(summary = "添加医生接口")
    @PostMapping("/insert")
    @SaCheckLogin
    public CommonResult insert(@RequestBody @Valid InsertDoctorForm form) {
        Map param = BeanUtil.beanToMap(form);
//        将tag转换为JSON类型
        String json = JSONUtil.parseArray(form.getTag()).toString();
        param.replace("tag", json);

        //生成uuid
        param.put("uuid", IdUtil.simpleUUID().toUpperCase());
        doctorService.insert(param);
        return CommonResult.ok();
    }

    @Operation(summary = "更新医生照片接口")
    @PostMapping("/updatePhoto")
    @SaCheckLogin
    public CommonResult updatePicture(@Param("file") MultipartFile file, @Param("doctorId") Integer doctorId) {
        doctorService.updatePicture(file, doctorId);
        return CommonResult.ok();
    }

    @Operation(summary = "回显要修改的医生信息")
    @PostMapping("/selectById")
    @SaCheckLogin
    public CommonResult selectById(@RequestBody @Valid SelectDoctorByIdForm form) {
        HashMap hashMap = doctorService.selectById(form.getId());
        return CommonResult.ok(hashMap);
    }

    @Operation(summary = "保存修改的医生信息")
    @PostMapping("/update")
    @SaCheckLogin
    public CommonResult update(@RequestBody @Valid UpdateDoctorForm form) {
        Map map = BeanUtil.beanToMap(form);
        String json = JSONUtil.parseArray(form.getTag()).toString();
        map.replace("tag", json);
        doctorService.update(map);
        return CommonResult.ok();
    }

    @Operation(summary = "删除医生信息接口")
    @PostMapping("/deleteDoctorByIds")
    @SaCheckLogin
    public CommonResult deleteByIds(@RequestBody @Valid DeleteDoctorByIdsForm form) {
        doctorService.deleteByIds(form.getIds());
        return CommonResult.ok();
    }

    @Operation(summary = "根据诊室id查询医生接口")
    @PostMapping("/selectDoctorsBySubId")
    @SaCheckLogin
    public CommonResult selectDoctorsBySubId(@RequestBody @Valid SelectDoctorsBySubIdForm form) {
        ArrayList<HashMap> result = doctorService.selectDoctorsBySubId(form.getDeptSubId());
        return CommonResult.ok().put("result", result);
    }
}
